package org.itboys.mobile.service.mongo.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.entity.mongo.SundayNews;
import org.itboys.mobile.entity.mongo.SundayNewsCategory;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/10/23
 * 描述：雄安加盟网_
 */
@Service
public class SundayNewsService extends BaseMongoService<SundayNews>{
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;
    @Autowired
    private SundayUploadService uploadService;
    @Autowired
    private ResourceHolder resourceHolder;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayNews> getEntityClass() {
        return SundayNews.class;
    }



    /**
     * 修改或新增
     * @param news
     * @param mageFile
     * @throws Exception
     */
    public void saveNews(SundayNews news, MultipartFile mageFile) throws  Exception{
        //保存主图
        Map<String, String> map = uploadService.uploadSingle(mageFile);
        if(StringUtils.isNotEmpty(map.get(uploadService.UPLOAD_HALF_PATH))){

            news.setImage(map.get(uploadService.UPLOAD_HALF_PATH));
        }
        if(news.getId()==0l){
            super.save(news);
        }else{
            super.updateExceptEmpty(news.getId(),news);
        }
    }

    /**
     * 分页获取
     * @param request
     * @return
     */
    public PageResult<SundayNews> select(HttpServletRequest request){
        Map<String,Object> param = ParamUtil.packageMongoExactParam(request,"categoryId");
        param.put("isDeleted", 0);
        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request,"title");
        PageResult<SundayNews> result=super.containpageQuery(request,containsparam,param);
        return result;
    }

    /**
     * 单个查找
     * @param id
     * @return
     */
    public SundayNews findOne(Long id){
        SundayNews news = null;

        if(LongUtil.isNotZreo(id)){

            news=super.getById(id);
            news.setImage(news.getImage());
            news.setImage(resourceHolder.getStringValue("imgRoot")+news.getImage());
        }else{
            news=new SundayNews();
        }
        return  news;
    }

    /**
     * 设置推荐
     * @param id
     * @param value
     */
    public void set(Long id,Integer value,Integer type){
        SundayNews news=super.getById(id);
        if(type==1){
            news.setIsRecommend(value);
        }
        if(type==2){
            news.setIsIndex(value);
        }
        super.updateExceptEmpty(id,news);
    }

}
